package com.ruoyi.supply.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.goods.domain.Goods;
import com.ruoyi.goods.service.IGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supply.domain.Supply;
import com.ruoyi.supply.service.ISupplyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供货Controller
 * 
 * @author yyg
 * @date 2023-11-27
 */
@Controller
@RequestMapping("/supply/supply")
public class SupplyController extends BaseController
{
    private String prefix = "supply/supply";

    @Autowired
    private ISupplyService supplyService;
    @Autowired
    private IGoodsService goodsService;
    @RequiresPermissions("supply:supply:view")
    @GetMapping()
    public String supply()
    {
        return prefix + "/supply";
    }

    /**
     * 查询供货列表
     */
    @RequiresPermissions("supply:supply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Supply supply)
    {
        startPage();
        List<Supply> list = supplyService.selectSupplyList(supply);
        return getDataTable(list);
    }

    /**
     * 导出供货列表
     */
    @RequiresPermissions("supply:supply:export")
    @Log(title = "供货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Supply supply)
    {
        List<Supply> list = supplyService.selectSupplyList(supply);
        ExcelUtil<Supply> util = new ExcelUtil<Supply>(Supply.class);
        return util.exportExcel(list, "供货数据");
    }

    /**
     * 新增供货
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供货
     */
    @RequiresPermissions("supply:supply:add")
    @Log(title = "供货", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Supply supply)
    {
      /*Long gId, String gName, String gType, BigDecimal gPurchasingprice, BigDecimal gPrice,
      Date gShelfLife, Long gNumber, String gImg, String gStatus, Long version,
      Date createDate, Long isDelete, String pName*/
     //   System.out.println(">>>>>>>>>>>>>当前登录用户>>>>>>>>>>>>>>>>"+ ShiroUtils.getLoginName());
        goodsService.insertGoods(new Goods(null,
                supply.getGoodsName(), null,
                supply.getGoodsPrice(), null,
                supply.getGoodsShelflife(),
                supply.getGoodsNumber(),
                supply.getGoodsImg(), null, null, null, null,
                ShiroUtils.getLoginName()
        ));

        return toAjax(supplyService.insertSupply(supply));
    }

    /**
     * 修改供货
     */
    @RequiresPermissions("supply:supply:edit")
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap)
    {
        Supply supply = supplyService.selectSupplyByGoodsId(goodsId);
        mmap.put("supply", supply);
        return prefix + "/edit";
    }

    /**
     * 修改保存供货
     */
    @RequiresPermissions("supply:supply:edit")
    @Log(title = "供货", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Supply supply)
    {
        return toAjax(supplyService.updateSupply(supply));
    }

    /**
     * 删除供货
     */
    @RequiresPermissions("supply:supply:remove")
    @Log(title = "供货", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(supplyService.deleteSupplyByGoodsIds(ids));
    }
}
